import { message } from 'antd';
import {
  reqGetRealSpeed,
  reqOneVehicle,
  reqGetLatestPosition,
  reqGetStatusDataRead,
  reqOpenOrRealDataRead,
} from './service';
export default {
  namespace: 'dataCollection',
  state: {
    vehicleInfo: {},
    status: false,
    postionData: [],
    speedData: [],
  },
  reducers: {
    setVehicle(state, { payload }) {
      return { ...state, vehicleInfo: payload };
    },
    setStatus(state, { payload }) {
      return { ...state, status: payload };
    },
    setPosition(state, { payload }) {
      return { ...state, postionData: payload };
    },
    setSpeed(state, { payload }) {
      return { ...state, speedData: payload };
    },
  },
  effects: {
    *fetchOne({ payload }, { call, put }) {
      const res = yield call(reqOneVehicle, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'setVehicle',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqGetStatusDataRead({ payload }, { call, put }) {
      const res = yield call(reqGetStatusDataRead, payload);
      try {
        if (res.code === 9) {
          yield put({
            type: 'setStatus',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqOpenOrRealDataRead({ payload }, { call, put }) {
      const url =
        'https://iot-pre.diwork.com/reader/api/v1/realTimeDataByTag?tags=cars@%E7%BB%8F%E5%BA%A6-14,cars@%E7%BB%8F%E5%BA%A6-13,cars@%E7%BB%8F%E5%BA%A6-12,cars@%E7%BB%8F%E5%BA%A6-11,cars@%E7%BB%8F%E5%BA%A6-10,cars@%E7%BB%8F%E5%BA%A6-9,cars@%E7%BB%8F%E5%BA%A6-8,cars@%E7%BB%8F%E5%BA%A6-7,cars@%E7%BB%8F%E5%BA%A6-6,cars@%E7%BB%8F%E5%BA%A6-5,cars@%E7%BB%8F%E5%BA%A6-4,cars@%E7%BB%8F%E5%BA%A6-3,cars@%E7%BA%AC%E5%BA%A6-14,cars@%E7%BA%AC%E5%BA%A6-13,cars@%E7%BA%AC%E5%BA%A6-12,cars@%E7%BA%AC%E5%BA%A6-11,cars@%E7%BA%AC%E5%BA%A6-10,cars@%E7%BA%AC%E5%BA%A6-9,cars@%E7%BA%AC%E5%BA%A6-8,cars@%E7%BA%AC%E5%BA%A6-7,cars@%E7%BA%AC%E5%BA%A6-6,cars@%E7%BA%AC%E5%BA%A6-5,cars@%E7%BA%AC%E5%BA%A6-4,cars@%E7%BA%AC%E5%BA%A6-3,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-14,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-13,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-12,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-11,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-10,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-9,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-8,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-7,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-6,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-5,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-4,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-3,cars@%E6%B8%A9%E5%BA%A6-14,cars@%E6%B8%A9%E5%BA%A6-13,cars@%E6%B8%A9%E5%BA%A6-12,cars@%E6%B8%A9%E5%BA%A6-11,cars@%E6%B8%A9%E5%BA%A6-10,cars@%E6%B8%A9%E5%BA%A6-9,cars@%E6%B8%A9%E5%BA%A6-8,cars@%E6%B8%A9%E5%BA%A6-7,cars@%E6%B8%A9%E5%BA%A6-6,cars@%E6%B8%A9%E5%BA%A6-5,cars@%E6%B8%A9%E5%BA%A6-4,cars@%E6%B8%A9%E5%BA%A6-3,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-14,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-13,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-12,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-11,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-10,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-9,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-8,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-7,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-6,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-5,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-4,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-3,cars@%E8%BD%AC%E9%80%9F-14,cars@%E8%BD%AC%E9%80%9F-13,cars@%E8%BD%AC%E9%80%9F-12,cars@%E8%BD%AC%E9%80%9F-11,cars@%E8%BD%AC%E9%80%9F-10,cars@%E8%BD%AC%E9%80%9F-9,cars@%E8%BD%AC%E9%80%9F-8,cars@%E8%BD%AC%E9%80%9F-7,cars@%E8%BD%AC%E9%80%9F-6,cars@%E8%BD%AC%E9%80%9F-5,cars@%E8%BD%AC%E9%80%9F-4,cars@%E8%BD%AC%E9%80%9F-3,cars@%E9%80%9F%E5%BA%A6-14,cars@%E9%80%9F%E5%BA%A6-13,cars@%E9%80%9F%E5%BA%A6-12,cars@%E9%80%9F%E5%BA%A6-11,cars@%E9%80%9F%E5%BA%A6-10,cars@%E9%80%9F%E5%BA%A6-9,cars@%E9%80%9F%E5%BA%A6-8,cars@%E9%80%9F%E5%BA%A6-7,cars@%E9%80%9F%E5%BA%A6-6,cars@%E9%80%9F%E5%BA%A6-5,cars@%E9%80%9F%E5%BA%A6-4,cars@%E9%80%9F%E5%BA%A6-3,cars@%E7%BB%8F%E5%BA%A6-2,cars@%E7%BA%AC%E5%BA%A6-2,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-2,cars@%E6%B8%A9%E5%BA%A6-2,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-2,cars@%E8%BD%AC%E9%80%9F-2,cars@%E9%80%9F%E5%BA%A6-2,cars@%E7%BB%8F%E5%BA%A6-1,cars@%E7%BA%AC%E5%BA%A6-1,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-1,cars@%E6%B8%A9%E5%BA%A6-1,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-1,cars@%E8%BD%AC%E9%80%9F-1,cars@%E9%80%9F%E5%BA%A6-1';
      const cookie =
        'ARK_ID=JSde7ae1b6061fcdc38a8a563bb2db91ebde7a; FZ_STROAGE.diwork.com=eyJTRUVTSU9OSUQiOiJlYjFlOTAwMTdhNTQ4NmU4IiwiU0VFU0lPTkRBVEUiOjE2Mjk1MTcxMDIzNjcsIkFOU0FQUElEIjoiNzlhZjc2Mjc4NDgyYmRiYiIsIkFOUyRERUJVRyI6MiwiQU5TVVBMT0FEVVJMIjoiaHR0cHM6Ly9hcnQuZGl3b3JrLmNvbS8iLCJGUklTVERBWSI6IjIwMjEwODIxIiwiRlJJU1RJTUUiOmZhbHNlLCJBUktfSUQiOiJKU2RlN2FlMWI2MDYxZmNkYzM4YThhNTYzYmIyZGI5MWViZGU3YSIsIkFSS0ZSSVNUUFJPRklMRSI6IjIwMjEtMDgtMjEgMTE6Mzg6MjEuNjM4IiwiQU5TU0VSVkVSVElNRSI6MH0%3D; at=518ce2a3-e08d-47b3-97c0-a21ea367b4ae; yonyou_uid=8620ea97-e454-4b6b-825d-11a0e18be45d; yonyou_uname=13308239129; yht_username_diwork=ST-13894-foMdi5mUGxIGKcHKxSSP-user-y3me-pre.diwork.com__8620ea97-e454-4b6b-825d-11a0e18be45d; yht_usertoken_diwork=WWJCCN9eaj2zM%2Fb3JT4nNDAUZJv5dPSN43clksq2bcx5U2FEFpocC1v3TAedd78DxVX5MGC8iGRjh8GjJ%2Bmkvg%3D%3D; yht_access_token=bttb3ZDTnY0UUdzS0xQSFlwTGZpd0xJQ1prQkVxWEQ3MnIzZXFidFpGUWlDTDJEL1FHUGhmVVVjbVQxZDRTd0x6ZTFBdnhiQjFkb05XQTFMMVVsN3RuVWVVNG41aVpxbFFybitzT2d0Qmh0ckdJVU01QVFKMWdEMnhCRkdDWmwrWWlfX3VzZXIteTNtZS1wcmUuZGl3b3JrLmNvbQ..__61ddc453cb427a1f090c4d9d8efacc4f_1631618735268; wb_at=LMjnpuvqjenFchrlN9wB9DbADwLLIjtrdqjxpldjoqdkchvnqjkbnl';
      const vehicleName = 'cars';
      const interval = 3;
      const vehicleIdList = new Array(payload.vehicleIdList);
      console.log(vehicleIdList);
      const formData = {
        url,
        cookie,
        vehicleName,
        interval,
        vehicleIdList: [...vehicleIdList],
      };
      const res = yield call(reqOpenOrRealDataRead, formData);
      try {
        if (res.code === 0) {
          message.success(res.msg);
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqGetLatestPosition({ payload }, { call, put }) {
      const res = yield call(reqGetLatestPosition, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'setPosition',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqGetRealSpeed({ payload }, { call, put }) {
      const res = yield call(reqGetRealSpeed, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'setSpeed',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
  },
};
